package com.trm.user.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.util.jdbc.BaseDao;

public class UserDao extends BaseDao {
    private int page = 1;
    private int pageSize = 10;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取用户列表
     * 
     * @return
     */
    public List<Map<String, Object>> getUserList() {
        List<Map<String, Object>> list = null;
        StringBuffer sql = new StringBuffer();
        sql.append("select stu.id, stu.name, stu.account, stu.sex, role.role");
        sql.append(" from student stu");
        sql.append(" left join role role on stu.role = role.id");
        sql.append(" limit " + pageSize * (page - 1) + "," + pageSize);
        list = this.executeSqlToList(sql.toString());
        return list;
    }

    /**
     * 根据角色获得用户数量
     * 
     * @param roleId
     * @return
     */
    public int getUserCountByRoleId(String roleId) {
        String sql = "select count(*) as count from student";
        if (roleId != null && !"".equals(roleId)) {
            sql += " where role = " + roleId;
        }
        Map<String, Object> countMap = this.executeSqlToMap(sql);
        int count = Integer.valueOf(countMap.get("count").toString());
        return count;
    }

    /**
     * 根据id查询用户信息
     * 
     * @param id
     */
    public Map<String, Object> getUserById(String id) {
        String sql = "select * from student where id = " + id;
        Map<String, Object> map = this.executeSqlToMap(sql);
        return map;
    }

    /**
     * 根据id修改用户信息
     * 
     * @param id
     * @return
     */
    public int updateUserById(Map<String, String> params) {
        String id = params.get("id");
        if (id == null || "".equals(id)) {
            System.out.println("未获取到id");
            return -1;
        }
        String column = "";
        for (Map.Entry<String, String> entry : params.entrySet()) {
            column += "," + entry.getKey() + " = '" + entry.getValue() + "'";
        }
        if ("".equals(column)) {
            System.out.println("请检查参数");
            return -1;
        }
        column = column.substring(1);
        String sql = "update student set " + column + " where id = " + id;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 添加用户
     * 
     * @param params
     * @return
     */
    public int addUser(Map<String, String> params) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        list.add(params);
        int result = this.insert(list, "student");
        return result;
    }

    /**
     * 修改密码
     * 
     * @param userId
     * @param password
     * @return
     */
    public int updatePassword(String userId, String password) {
        String sql = "update student set password = '" + password + "' where id = " + userId;
        int result = this.executeSql(sql);
        return result;
    }

    /**
     * 查询密码
     * 
     * @param id
     * @param password
     * @return
     */
    public int selectPassword(String id, String password) {
        String sql = "select count(id) as count from student where id = " + id + " and password = '" + password + "'";
        Map<String, Object> map = this.executeSqlToMap(sql);
        int count = 0;
        if (map != null) {
            count = Integer.valueOf(map.get("count").toString());
        }
        return count;
    }

    /**
     * 查询账号在系统中是否存在
     * 
     * @param id
     * @param account
     * @return
     */
    public int selectAccount(String id, String account) {
        String sql = "select count(id) as count from student where account = '" + account + "'";
        if (id != null && !"".equals(id)) {
            sql += "and id <> " + id + "";
        }
        Map<String, Object> map = this.executeSqlToMap(sql);
        int count = 0;
        if (map != null) {
            count = Integer.valueOf(map.get("count").toString());
        }
        return count;
    }
}
